package demo.practice.leetcode_eveyday;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Stack;

public class P830 {

    public static void main(String[] args) {
        P830 p830= new P830();
        p830.largeGroupPositions("abbxxxxzzy");
    }

    public List<List<Integer>> largeGroupPositions(String s) {
        List<List<Integer>> target = new ArrayList<>();
        if(s==null || s.length()==0)
            return target;

        int findex=0;
        char befor=s.charAt(0);
        int time=1;
        for(int i=1;i<s.length();i++){
            if(s.charAt(i)==befor){
                time++;
            }else{
                if(time>=3){
                    target.add(Arrays.asList(findex, i-1));
                }
                findex=i;
                befor=s.charAt(i);
                time=1;
            }
        }

        if(time>=3){
            target.add(Arrays.asList(findex, s.length()-1));
        }

        return target;

    }

    public List<List<Integer>> largeGroupPositions1(String s) {
        List<List<Integer>> target = new ArrayList<>();
        if(s==null || s.length()==0)
            return target;

        Stack<Character> stack = new Stack<>();
        int findex = 0;
        stack.add(s.charAt(0));
        for (int i = 1; i < s.length(); i++) {
            if (stack.peek() == s.charAt(i)) {
                stack.add(s.charAt(i));
            } else {
                if (stack.size() >= 3) {
                    target.add(Arrays.asList(findex, i-1));
                }
                stack = new Stack<>();
                stack.add(s.charAt(i));
                findex = i;
            }
        }

        if(stack.size() >= 3)
            target.add(Arrays.asList(findex, s.length()-1));
        return target;
    }

}
